import 'package:args/args.dart';

class CliOptions {
  final String url;
  final String dir;
  final int threads;
  final int retries;
  final Duration? timeout;
  final bool dryRun;

  CliOptions({
    required this.url,
    required this.dir,
    this.threads = 3,
    this.retries = 3,
    this.timeout,
    this.dryRun = false,
  });

  static CliOptions parse(List<String> args) {
    final parser = ArgParser()
      ..addOption('url',
          abbr: 'u', help: 'Base URL for update.json', mandatory: true)
      ..addOption('dir',
          abbr: 'd', help: 'Download root directory', mandatory: true)
      ..addOption('threads',
          abbr: 't',
          help: 'Max concurrent downloads',
          defaultsTo: '3')
      ..addOption('retries',
          abbr: 'r', help: 'Max retry times', defaultsTo: '3')
      ..addOption('timeout',
          abbr: 'T', help: 'Timeout in seconds (optional)')
      ..addFlag('dry-run',
          abbr: 'n', help: 'Dry run mode (no actual download/unzip)', negatable: false);

    final results = parser.parse(args);

    final url = results['url'] as String;
    final dir = results['dir'] as String;
    final threads = int.parse(results['threads'] as String);
    final retries = int.parse(results['retries'] as String);
    Duration? timeout;
    if (results['timeout'] != null) {
      timeout = Duration(seconds: int.parse(results['timeout'] as String));
    }
    final dryRun = results['dry-run'] as bool;

    return CliOptions(
      url: url,
      dir: dir,
      threads: threads,
      retries: retries,
      timeout: timeout,
      dryRun: dryRun,
    );
  }
}